Voorbeeld van de functie GetObject

Dit voorbeeld maakt gebruik van de functie GetObject om een verwijzing naar een specifiek werkblad van Microsoft Excel (MyXL) te verkrijgen. Het maakt gebruik van de eigenschap Application van het werkblad om Microsoft Excel zichtbaar te maken, te sluiten, enzovoort. Als er gebruik wordt gemaakt van API-oproepen, zoekt de Sub-procedure DetextExcel naar Microsoft Excel. Als dit programma actief is, wordt het in de tabel met actieve objecten ingevoerd. De eerste oproep van GetObject veroorzaakt een fout als Microsoft Excel nog niet is geactiveerd. In het voorbeeld is de fout er oorzaak van dat de ExcelWasNotRunning-vlag op Waar wordt ingesteld. Bij de tweede oproep van GetObject wordt gespecificeerd dat een bestand wordt geopend Als Microsoft Excel nog niet wordt uitgevoerd, wordt de tweede oproep gestart. Deze geeft als resultaat een verwijzing naar het werkblad dat wordt weergegeven door het opgegeven bestand, mytest.xls. Het bestand moet in de opgegeven locatie staan, anders wordt een automatiseringsfout van Visual Basic gegenereerd. Vervolgens maakt de programmacode zowel Microsoft Excel als het opgegeven werkblad zichtbaar. Ten slotte, als er geen eerdere versie van Microsoft Excel actief was, maakt de programmacode gebruik van de methode Quit van het object Application om Microsoft Excel af te sluiten. Als de toepassing reeds actief was, wordt niet geprobeerd Microsoft Excel af te sluiten. De verwijzing zelf wordt vrijgegeven door deze op Nothing in te stellen.

' Declare necessary API routines:
Declare Function FindWindow Lib "Gebruiker32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
                    ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "Gebruiker32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
                    ByVal wParam as Long, _
                    ByVal lParam As Long) As Long

Sub GetExcel()
    Dim MyXL As Object    ' Variable to hold reference
                                ' to Microsoft Excel.
    Dim ExcelWasNotRunning As Boolean    ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
    On Error Resume Next    ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
    Set MyXL = Getobject(, "Excel.Application")
    If Err.Number <> 0 Then ExcelWasNotRunning = True
    Err.Clear    ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
    DetectExcel

' Set the object variable to reference the file you want to see.
    Set MyXL = Getobject("c:\vb4\MIJNTEST.XLS")

' Show Microsoft Excel through its Application property. Dan
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
    MyXL.Application.Visible = True
    MyXL.Parent.Windows(1).Visible = True
     Do manipulations of your  file here.
    ' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the 
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
    If ExcelWasNotRunning = True Then
        MyXL.Application.Quit
    End IF

    Set MyXL = Nothing    ' Release reference to the
                                ' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
    Const WM_USER = 1024
    Dim hWnd As Long
' If Excel is running this API call returns its handle.
    hWnd = FindWindow("XLMAIN", 0)
    If hWnd = 0 Then    ' 0 means Excel not running.
        Exit Sub
    Else                
    ' Excel is running so use the SendMessage API
    ' function to enter it in the Running Object Table.
        SendMessage hWnd, WM_USER + 18, 0, 0
    End If
End Sub